home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / tools / makemap.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  252 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * makemap - Reload the colormap
  19.  *
  20.  * Nowadays, GL programs share a common X colormap, which is distinct
  21.  * from the default root colormap.  makemap causes new colors to be
  22.  * stored into that colormap.
  23.  *
  24.  * By default, makemap will initialize all colors from 0-255 except
  25.  * for color cells 16-31.
  26.  *
  27.  * Usage: makemap [-full] [-ignore16to31]
  28.  * Options:
  29.  *    -full        In addition to the default behavior,
  30.  *            write a color cube into 256-512,
  31.  *            and a gray ramp into 513-639.
  32.  *
  33.  *    -ignore16to31    Don't bother trying to fixup cells 16-31;
  34.  *            Just leave these cells alone.
  35.  */
  36. #include <X11/Xlib.h>
  37. #include <gl.h>
  38. #include <sys/types.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #define NOGREYPATTERNS
  42. #include "colortbl.inc"
  43.  
  44. flushAndExit(int code)
  45. {
  46.     gflush();
  47.     exit(code);
  48. }
  49.  
  50. main(argc, argv)
  51.     int argc;
  52.     char **argv;
  53. {
  54.     char *progname = argv[0];
  55.     int i, j, v, r, g, b, w;
  56.     char *cp;
  57.     int ramp, planes;
  58.     int do_fullmap = 0;
  59.     int ignore16to31 = 0;
  60.  
  61.     int illegal_args = 0;
  62.     int rgb8bit = 0;
  63.  
  64.     while ((--argc > 0) && (*(cp = *++argv) == '-')) {
  65.     ++cp;
  66.     if (strcmp(cp, "full") == 0)
  67.         do_fullmap = 1;
  68.     else if (strncmp(cp, "ignore", 6) == 0)
  69.         ignore16to31 = 1;
  70.     else
  71.         ++illegal_args;
  72.     }
  73.  
  74.     if (argc > 0 || illegal_args) {
  75.     fprintf(stderr, "%s: usage: %s [-full] [-ignore16to31]\n",
  76.             progname, progname);
  77.     exit(1);
  78.     }
  79.  
  80.     noport();
  81.     foreground();
  82.     winopen("makemap");
  83.  
  84.     /*
  85.      * Write to color cells 0-15 and 16-255.
  86.      */
  87.  
  88.     for(i=0; i<16; i++)
  89.     gammapcolor(i,red_map[i],green_map[i],blue_map[i]);
  90.  
  91.     /* leave the X part of the map alone */
  92.  
  93.     for(i=32; i<256; i++)
  94.     gammapcolor(i,red_map[i],green_map[i],blue_map[i]);
  95.  
  96.     /*
  97.      * Write the minimum #colors to cells 16-31,
  98.      * so as to make 16-31 match the default root X colormap.
  99.      */
  100.     if (!ignore16to31)
  101.     fixup16to31();
  102.  
  103.     if (do_fullmap &&
  104.     ((planes = getgdesc(GD_BITS_NORM_SNG_CMODE)) <= 8))
  105.     flushAndExit(1);
  106.  
  107.     rgb8bit = (getgdesc(GD_BITS_NORM_SNG_RED) == 3);
  108.  
  109.     if (!do_fullmap && !rgb8bit)
  110.     flushAndExit(0);        /* we're done */
  111.  
  112.     if (do_fullmap) {
  113.     /*
  114.      * Make an ordered color ramp at 256
  115.      */
  116.     for (i=0; i<256; i++) {
  117.         r = (i>>0) & 7;
  118.         g = (i>>3) & 7;
  119.         b = (i>>6) & 3;
  120.         r = (255*r)/7;
  121.         g = (255*g)/7;
  122.         b = (255*b)/3;
  123.         gammapcolor(i+256,r,g,b);
  124.     }
  125.     /*
  126.      * Make a gray ramp of 128 entries
  127.      */
  128.     ramp = greybase();
  129.     for (i=0; i<128; i++) 
  130.         gammapcolor(i+ramp,i<<1,i<<1,i<<1);
  131.     }
  132.  
  133.     /*
  134.      * For 8-bit RGB systems, load 8-bit RGB ramp at top of color map
  135.      */
  136.     if (rgb8bit) {
  137.     w = 0;
  138.     for(i=0; i<4; i++) { 
  139.         b = i<<6 | i<<4 | i<<2 | i;
  140.         for(j=0; j<8; j++) {
  141.         g = j<<5 | j<<2 | j>>1;
  142.         for(v=0; v<8; v++) {
  143.             r = v<<5 | v<<2 | v>>1;
  144.             gammapcolor(3840+w,r,g,b);
  145.             w++;
  146.         }
  147.         }
  148.     }
  149.     }
  150.     flushAndExit(0);
  151. }
  152.  
  153. #define IS_WRITTEN (1<<24)    /* indicates that cell has been written to */
  154. #define PACK_RGBF(r,g,b,f) ((r) | ((g) << 8) | ((b) << 16) | (f))
  155. #define UNPACK_R(rgbf) ((rgbf) & 0xff)
  156. #define UNPACK_G(rgbf) (((rgbf) >> 8) & 0xff)
  157. #define UNPACK_B(rgbf) (((rgbf) >> 16) & 0xff)
  158. #define WAS_ALLOCED (1<<25)    /* only used for code in fixup16to31 */
  159. /*
  160.  * Fix up color cells 16-31.
  161.  *
  162.  * NOTE:  The following code is very temporary,
  163.  *        and WILL be changed soon.
  164.  */
  165. fixup16to31()
  166. {
  167.     short r, g, b;
  168.     unsigned int *cells = (unsigned int *) 0;
  169.     Display *dpy;
  170.     Colormap cmap;
  171.     int i, depth, screen;
  172.     unsigned long dummycell, tmp_cells[32];
  173.     
  174.     XColor pix[16];
  175.     int cellFlags[16];
  176.     int nGLcellsWrittenTo = 0;
  177.     int n = 0;        /* number cells we've alloced (& need to free) */
  178.     /*
  179.      * First find which which of the cells 16-31 in the GL colormap
  180.      * have ever been written to.
  181.      */
  182.     getmcolor(0xfade, &r, &g, &b);
  183.     if ((u_short) b == 0xabcd) {
  184.     int *data = (int *) (((r << 16) & 0xffff0000) | (u_short) g);
  185. #define _GL_CDATA_Addr        0
  186. #define _GL_CDATA_ShmCells    1
  187. #define _GL_CDATA_Flags        2
  188. #define _GL_CDATA_Cmap        3
  189. #define _GL_CDATA_Visual    4
  190. #define _GL_CDATA_Root        5
  191. #define _GL_CDATA_Dpy        6
  192. #define _GL_CDATA_Screen    7
  193.     if (data && data[_GL_CDATA_Addr] == (int) &data[_GL_CDATA_Addr]) {
  194.         cells = (unsigned int *) data[_GL_CDATA_ShmCells];
  195.         dpy = (Display *) data[_GL_CDATA_Dpy];
  196.         screen = data[_GL_CDATA_Screen];
  197.     }
  198.     }
  199.     if (!cells || !dpy)
  200.     return;
  201.     depth = DefaultDepth(dpy, screen);
  202.     if (depth == 24)
  203.     return;
  204.     cmap = DefaultColormap(dpy, screen);
  205.     for (i = 0; i < 16; ++i) {
  206.     cellFlags[i] = WAS_ALLOCED;    /* initialize */
  207.     pix[i].pixel = i + 16;        /* for X calls which follow */
  208.      if (cells[i+16] & IS_WRITTEN) {
  209.         ++nGLcellsWrittenTo;
  210.         cellFlags[i] |= IS_WRITTEN;
  211.      }
  212.     }
  213.     if (nGLcellsWrittenTo == 0) {
  214.     /*
  215.      * None of 16-31 were written to in GL colormap.
  216.      * There's nothing to do.
  217.      */
  218.     return;
  219.     }
  220.     /*
  221.      * Now find which cells 16-31 are allocated
  222.      * in the X default root colormap.
  223.      */
  224.     dummycell = 0;
  225.     while (dummycell < 31) {
  226.     XAllocColorCells(dpy, cmap, True, 0, 0,
  227.             (unsigned long *) &dummycell, 1);
  228.     if (dummycell == 0)
  229.         break;        /* alloc failed */
  230.     tmp_cells[n++] = dummycell;    /* keep track of what we've done */
  231.     if (15 < dummycell && dummycell < 32)
  232.         cellFlags[dummycell - 16] &= ~WAS_ALLOCED;
  233.     }
  234.     if (n)
  235.     XFreeColors(dpy, cmap, tmp_cells, n, 0);
  236.     else
  237.     return;
  238.     XQueryColors(dpy, cmap, &pix[0], 16);
  239.     /*
  240.      * For cells 16-31, if the cell has been written to in
  241.      * in the GL colormap (via mapcolor), and if the corresponding
  242.      * cell in the X root colormap is allocated, then take what's
  243.      * in that X cell and put it into the GL cell.
  244.      */
  245.     for (i = 0; i < 16; ++i) {
  246.     if ((cellFlags[i] & (IS_WRITTEN | WAS_ALLOCED))
  247.             == (IS_WRITTEN | WAS_ALLOCED))
  248.         mapcolor(i+16, pix[i].red >> 8,
  249.             pix[i].green >> 8, pix[i].blue >> 8);
  250.     }
  251. }
  252.